<template>
  <!-- 设备管理 -->
  <div v-loading="loading" element-loading-text="拼命加载中">
    <el-row>
      <!-- 按钮区 -->
      <el-col :span="24">
        <div class="button-wrapper">
          <el-button type="primary" style="vertical-align:middle;margin-top:10px;margin-left: 5px" round size="mini" @click=handleAdd()>新增</el-button>
          <el-button type="primary" style="vertical-align:middle;margin-top:10px;margin-left: 5px" round size="mini" @click=handUpdate()>修改</el-button>
          <el-button type="danger" style="vertical-align:middle;margin-top:10px;" round size="mini" @click="batchEdit(-1)">删除</el-button>
        </div>
      </el-col>
    </el-row>
    <!-- 表格 -->
    <el-row>
      <el-col :span="24">
        <div class="data-wrapper">
          <template v-if="tableData!=null">
            <el-table ref="multipleTable" :data="tableData"  tooltip-effect="dark" style="width: 100%"
                      @selection-change="handleSelectionChange">
              <el-table-column type="selection" label="全选" width="60">
              </el-table-column>
              <el-table-column type="index" label="序号" :index="indexMethod" width="60">
              </el-table-column>
              <el-table-column prop="linkName" label="链接名字" width="200">
              </el-table-column>
              <el-table-column prop="linkUrl" label="链接路径" width="200">
              </el-table-column>
              <el-table-column prop="linkLocation" label="链接位置" width="200">
              </el-table-column>
              <el-table-column fixed="right" :show-overflow-tooltip="true"  prop="linkNote" label="链接备注" align="center" >
              </el-table-column>
            </el-table>
          </template>
        </div>
      </el-col>
    </el-row>
    <!-- 底部按钮和分页 -->
    <el-row :gutter="20" v-if="dataCount" type="flex" justify="space-between" align="center" class="page-wrapper">
      <el-col :span="24">
        <div class="page-item">
          <el-pagination background
                         @size-change="handleSizeChange"
                         @current-change="handleCurrentChange"
                         :current-page.sync="listQuery.currentPage"
                         :page-sizes="pageSizes"
                         :page-size="listQuery.pageSize"
                         layout="total, sizes, prev, pager, next, jumper"
                         :total="dataCount"/>
        </div>
      </el-col>
    </el-row>

    <!-- 新增修改弹窗 -->
    <el-dialog title="新增设备" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form">
        <el-row :gutter="5">
          <el-col :span="12">
            <el-form-item label="链接名字" :label-width="formLabelWidth" prop="linkName">
              <el-input v-model="form.linkName" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="5">
          <el-col :span="12">
            <el-form-item label="链接路径" :label-width="formLabelWidth" prop="linkUrl">
              <el-input v-model="form.linkUrl" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="5" style="margin-top: 18px">
          <el-col :span="12">
            <el-form-item label="链接位置" :label-width="formLabelWidth" prop="linkLocation">
              <el-input v-model="form.linkLocation" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row :gutter="5">
          <el-col :span="24">
            <el-form-item label="链接备注" :label-width="formLabelWidth" prop="newsContents">
              <el-input type="textarea" v-model="form.linkNote" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="medium" @click="dialogFormVisible = false">取 消</el-button>
        <el-button size="medium" type="success" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
    export default {
        data(){
            return {
                tableData:null,
                loading:false,
                multipleSelection: [],
                //默认数据总数
                dataCount:0,
                pageSizes:[5, 10, 20, 30, 40],
                //查询条件
                listQuery: {
                    currentPage:1,
                    pageSize:5
                },
                formLabelWidth: '120px',
                dialogFormVisible:false,
                //声明的一个单行表格数据
                oneCol:{
                },
                form:{},
                rules: {
                    linkName:[
                        { required: true, message: '请输入衔接名称', trigger: 'blur' }
                    ],
                    linkUrl:[
                        { required: true, message: '请输入链接路径', trigger: 'blur' }
                    ],
                    linkLocation:[
                        { required: true, message: '请输入链接位置', trigger: 'blur' }
                    ],
                    linkNote:[
                        { required: true, message: '请输入链接备注', trigger: 'blur' }
                    ]
                }
            }
        },
        mounted(){
            this.getList();
        },
        methods:{
            getList() {
                this.loading=true;
                this.postRequest('/api/rcs/friendLinkss',this.listQuery).then((res)=>{
                    var data = res.data;
                    console.log(data);
                    if(data.status == 200){
                        this.tableData=data.data
                        this.dataCount=data.count;
                        this.loading = false;
                    }else{
                        this.$message.error(data.msg);
                    }
                })
                    .catch((error)=>{
                        //错误信息
                        this.$message.error(error);
                    })
            },
            //为空判断
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            indexMethod(index) {
                return (index+1)+(this.listQuery.currentPage-1)*this.listQuery.pageSize;
            },
            handleSizeChange(size) {
                this.listQuery.pageSize=size;
                this.listQuery.currentPage=1;
                this.getList()
            },
            handleCurrentChange(currentPage) {
                this.listQuery.currentPage=currentPage;
                this.getList()
            },
            //修改操作
            handUpdate(){
                if(this.multipleSelection.length==1){
                    this.form=this.multipleSelection[0];
                    this.dialogFormVisible=true;
                }else{
                    this.$message.error("请选择一条数据！");
                }
            },
            //增加
            handleAdd(){
                this.form={};
                this.dialogFormVisible=true;
            },
            //批量操作
            batchEdit(val) {
                if(this.multipleSelection.length <= 0){
                    this.$message.warning('请至少选择一条数据！');
                    return;
                }
                var strArr = [];
                for (var i = 0; i < this.multipleSelection.length; i++) {
                    strArr.push(this.multipleSelection[i].linkId);
                }
                //前面三个参数分别是 表名，状态字段名，id字段名
                //status 为状态值 （0：正常  9：停用 -1：删除）
                //ids 为需要批量操作的 id数组
                var submitData = {
                    tableName: "links",
                    tableCol: "state",
                    tableId: "link_id",
                    status: val,
                    ids: strArr
                };
                this.$confirm("确认批量操作？").then(_ => {
                    this.putRequest("/api/common/batch", submitData)
                        .then(res => {
                            var data = res.data;
                            if (data.status == 200) {
                                this.$message.success(data.msg);
                                this.getList();
                            } else {
                                this.$message.error(data.msg);
                            }
                        })
                        .catch(error => {
                            //错误信息
                            this.$message.error(error);
                        });
                });
            },
            //新增  保存
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        var url = "/api/rcs/operateLink";
                        this.postRequest(url,this.form).then((res)=>{
                            if(res.data.status == 200){
                                this.dialogFormVisible = false;
                                this.$message({
                                    message: '操作成功！',
                                    type: 'success'
                                });
                                this.getList()
                            }else{
                                this.$message.error("操作失败！");
                            }
                        }).catch((error)=>{
                                //错误信息
                                this.$message.error(error)
                            })
                    } else {
                        this.$message.error('error submit!!');
                        return false;
                    }
                });
            },
        },
    }
</script>
<style scoped>

</style>